package com.akillitavsiye;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.persistence.Id;

import com.akillitavsiye.datastore.UserDAO;
import com.google.appengine.api.datastore.QueryResultIterator;
import com.googlecode.objectify.Query;

public @SessionScoped @Named
class User implements Cloneable, Serializable {
	private static final long serialVersionUID = -3109846197586782365L;
	
	@Id private String username;

	private String name;
	private String surname;

	private String gender;
	private Date dateOfBirth;
	private String birthDate;
	
	private String email;
	
	private Date registrationDate;
	private Date lastUpdateDate;
	
	private void cleanFields() {
		this.username = "";
		this.name = "";
		this.surname = "";
		this.email = "";
		this.dateOfBirth = null;
		this.gender = "";
	}
	
	public String createNewUser(){
		UserDAO userDAO = UserDAO.getInstance();

		if (username != null && !username.isEmpty()){
			this.registrationDate = Calendar.getInstance().getTime();
			this.lastUpdateDate = Calendar.getInstance().getTime();
			userDAO.put(this);
			
			cleanFields();
		}
		
		return "";
	}
	
	public String deleteUser(){
		UserDAO userDAO = UserDAO.getInstance();
		User user = userDAO.get(this.username);
		userDAO.delete(user);
		this.username = "";
		return "";
	}

	public String getBirthDate() {
		return birthDate;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public String getEmail() {
		return email;
	}

	public String getGender() {
		return gender;
	}

	public Date getLastUpdateDate() {
		return lastUpdateDate;
	}

	public String getName() {
		return name;
	}

	public Date getRegistrationDate() {
		return registrationDate;
	}

	public String getSurname() {
		return surname;
	}

	public String getUsername() {
		return username;
	}
	
	public String getUsers(){
		String usernames = "";

		UserDAO userDAO = UserDAO.getInstance();
		Query<User> query = userDAO.ofy().query(User.class);
		QueryResultIterator<User> iterator = query.iterator();
		
		while (iterator.hasNext()) {
			User user = (User) iterator.next();
			usernames += user.getUsername() + " \n ";
		}
		
		return usernames;
	}

	public void setBirthDate(String birthDate) {
		this.birthDate = birthDate;

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		try {
			this.dateOfBirth = dateFormat.parse(birthDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public void setLastUpdateDate(Date lastUpdateDate) {
		this.lastUpdateDate = lastUpdateDate;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setRegistrationDate(Date registrationDate) {
		this.registrationDate = registrationDate;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}
